package com.zsh.weather.ln.home.ui.search

import android.os.Handler
import android.os.Message
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.yechaoa.yutilskt.ToastUtilKt
import com.zsh.weather.ln.R
import com.zsh.weather.ln.base.BaseMVPFragment
import com.zsh.weather.ln.bean.CityHotList
import com.zsh.weather.ln.bean.CityInfo
import com.zsh.weather.ln.bean.CitySearchList
import com.zsh.weather.ln.home.adapter.SearchAdapter
import com.zsh.weather.ln.home.ui.search.contract.SearchConstract
import com.zsh.weather.ln.home.ui.search.presenter.SearchPresenter
import com.zsh.weather.ln.utils.Utils

class SearchFragment: BaseMVPFragment<SearchConstract.View ,SearchPresenter>(),SearchConstract.View {
    private lateinit var tv_search: TextView
    private lateinit var et_view: EditText
    private lateinit var rv_view: RecyclerView
    private lateinit var adapter: SearchAdapter
    override fun createPresenter(): SearchPresenter {
        return SearchPresenter()
    }
    var mHandler = object : Handler(){
        override fun handleMessage(msg: Message) {
            when(msg.what){
                1 ->{
                    var cityBase = msg.obj as CityHotList
                    adapter = SearchAdapter(cityBase.topCityList)
                    rv_view.adapter = adapter
                    click()
                }
                2 ->{
                    ToastUtilKt.showToast("请查看搜索结果")
                    var citySearchList = msg.obj as CitySearchList
                    adapter.data = citySearchList.location
                    adapter.notifyDataSetChanged()
                }
            }
        }
    }
    private fun click(){
        tv_search.setOnClickListener {
            var str = et_view.text
            if(str.isBlank()){
                ToastUtilKt.showToast("请输入搜索城市")
                return@setOnClickListener
            }
            Utils.inputKey(mContext)
            presenter.startSearch(str.toString())
        }
        adapter.setOnItemClickListener(object : OnItemClickListener{
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                var cityInfo = adapter.data.get(position) as CityInfo
                Utils.openUrl(mContext,cityInfo.fxLink,cityInfo.name)
            }
        })
    }

    override fun searchResult(citySearchList: CitySearchList) {
        var msg = Message.obtain()
        msg.what = 2
        msg.obj = citySearchList
        mHandler.sendMessage(msg)
    }

    override fun getHotCitys(cityHot: CityHotList) {
        var msg = Message.obtain()
        msg.what = 1
        msg.obj = cityHot
        mHandler.sendMessage(msg)
    }
    override fun getLayoutId(): Int {
        return R.layout.fragment_search_city
    }

    override fun initView(v: View) {
        et_view = v.findViewById(R.id.et_view)
        tv_search = v.findViewById(R.id.tv_search)
        rv_view = v.findViewById(R.id.rv_view)
        rv_view.layoutManager = LinearLayoutManager(mContext)
    }

    override fun initData() {
        super.initData()
        presenter.getHotCitys()
    }
    override fun error(msg: String) {
        ToastUtilKt.showToast(msg)
    }

    override fun hideLoading() {
    }

    override fun showLoading() {
    }
}